<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Mail;
use DB;
use Crypt;

class ResetController extends Controller
{
    //forget
    public function forget()
    {
    	return view('home.reset.forget');
    }

    //reset
    public function reset()
    {
    	return view('home.reset.reset');
    }
    public function checkuser(Request $request)
    {

        $data = $request -> except('_token');
        //查询数据库是否有该用户名
       
       $res = DB::table('user_detail') -> where('email',$data['email']) -> first();

        if(!$res)
        {
            return $msg = '该邮箱不存在';
        }else{
            return $msg = null;
        }

    }
    //send
    public function send(Request $request)
    {
    	// dd($request -> all());

    	
		// Mail::raw('你好啊，', function ($message) {
		// 	$message -> subject('这是一封来自于LAMP139的邮件');
		// 	$message -> to('1041898222@qq.com');
		// });
		
		
		
		// Mail::send('mails.send',['username' => '张总'],function($message) use ($request){
		// 	$message -> subject('这是一封来自于LAMP139的邮件');
		// 	$message -> to($request -> input('email'));
		// });
		
        // 根据邮箱查询
		$data = DB::table('user_detail as d') -> leftJoin('user','user.id','=','d.uid') -> where('email',$request -> input('email')) -> first();
		// dd($data);
		//判断邮箱是否存在
		if(!$data)
		{
			return back();
		}
        // Mail::raw('sadkjfsd',function($message){
        //     $message -> subject('asdkfj');
        //     $message -> to('1041898222@qq.com');
        // });
        // $flag = Mail::send('mails.send',['data' => $data],function($message) use ($request){
        //  $message -> subject('这是一封来自于LAMP139的邮件');
        //  $message -> to($request -> input('email'));
        // });

       //发送邮件
		$flag = Mail::send('mails.send',['data' => $data],function($message) use ($request){
            $message -> subject('美丽说重置密码');
			$message -> to($request -> input('email'));
		});
		if($flag)
		{
			return redirect('/info');
		}else
		{
			return back('/reset');
		}
		
    }

    //info
    public function info()
    {
    	return view('home.reset.info');
    }

    //newform
    public function newform($id, $token)
    {
        //根据id与token判断是否来源于邮件
    	$data = DB::table('user') -> where('id',$id) -> where('token',$token) ->first();
    	if($data)
    	{
    		return view('home.reset.newform',['data' => $data]);
    	}else
    	{
    		return redirect('/info');
    	}
    	
    }

    //update
    public function update(Request $request)
    {
        //获取新密码
    	$data = $request -> only('password');
        // dd($data);
     $this -> validate($request,[
        'password' => 'min:6',
        'repassword' => 'same:password',
        ],[
            'passowrd.min' => '密码最少6位',
            'repassword.same' => '密码与确认密码不一致',
        ]
        );

        //密码加密
        $data['password'] = Crypt::encrypt($data['password']);
        //重设密码
    	DB::table('user') -> where('id',$request -> input('id')) -> update($data);
        return redirect('/success');
    }
    public function success()
    {
        return view('home.reset.success');
    }
}
